package MapReduce.writableBean;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/16 9:14
 */
public class PhoneTrafficWritable implements WritableComparable<PhoneTrafficWritable> {

    private int upFlow;
    private int downFlow;
    private int sumFlow;

    public int getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(int upFlow) {
        this.upFlow = upFlow;
    }

    public int getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(int downFlow) {
        this.downFlow = downFlow;
    }

    public int getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(int sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public void write(DataOutput out) throws IOException {

        out.writeInt(upFlow);
        out.writeInt(downFlow);
        out.writeInt(sumFlow);

    }

    //反序列化的时候，读取字段的顺序必须与序列化时字段的顺序保持一致
    @Override
    public void readFields(DataInput in) throws IOException {
        upFlow = in.readInt();
        downFlow = in.readInt();
        sumFlow = in.readInt();
    }

    @Override
    public String toString() {
        return upFlow+"\t"+downFlow+"\t"+sumFlow;
    }

    @Override
    public int compareTo(PhoneTrafficWritable o) {

        int thisSumFlow = this.sumFlow;
        int thatSumFlow = o.getSumFlow();

        int thisUpFlow = this.upFlow;
        int thatUpFlow = o.getUpFlow();

        if(thisSumFlow == thatSumFlow){
            return thatUpFlow-thisUpFlow;
        }else{
            return thatSumFlow-thisSumFlow;
        }

    }
}
